/**
 * 封装axios请求
 */
import axios from "axios";
import { createDiscreteApi } from "naive-ui";
const { message } = createDiscreteApi(["message"]);
// import { baseUrl, serverPath } from "./apiBaseConfig";
// 自定义一个axios实例
// console.log('process.env.NODE_ENV', process.env)
const instance = axios.create({
  baseURL: '', //http://localhost:8083
  timeout: 100000,
  headers: {
    // "content-type": "application/json",
  },
});
// 请求拦截
instance.interceptors.request.use(
  (originalRequest) => {
    // const token = localStorage.getItem("token");
    // if (token) {
    //   const headers = originalRequest.headers || {};
    //   headers["token"] = token;
    //   originalRequest.headers = headers;
    // }
    return Promise.resolve(originalRequest);
  },
  (error) => {
    return Promise.reject(error);
  },
);

// 响应拦截
instance.interceptors.response.use(
  (response: any) => {
    if (response.data.code && response.data.code === '00000')
    {
      return Promise.resolve(response.data);
    } else if (response.request.responseType === "blob") {
      return response;
    } else {
      message.error(response.data.message || response.data.msg);
      return Promise.reject(response.data);
    }
  },
  (error) => {
    console.log('error', error)
    if (error.message === "timeout of 5000ms exceeded") {
        message.error("网络异常，请刷新浏览器");
    } else {
      message.error(error.message || error.msg);
    }
    return Promise.reject(error);
  },
);

export default instance;
